package com.tongtech.system.controller;

import com.tongtech.common.core.constant.UserConstants;
import com.tongtech.common.core.utils.StringUtils;
import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.Industry;
import com.tongtech.system.service.IIndustryService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行业信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/industry")
public class IndustryController extends BaseController
{
    @Autowired
    private IIndustryService industryService;

    /**
     * 获取行业列表
     */
    @RequiresPermissions("basicdata:industry:list")
    @GetMapping("/list")
    public AjaxResult list(Industry industry)
    {
        List<Industry> industrys = industryService.selectIndustryList(industry);
        return success(industrys);
    }

    /**
     * 查询行业列表（排除节点）
     */
    @RequiresPermissions("basicdata:industry:list")
    @GetMapping("/list/exclude/{industryId}")
    public AjaxResult excludeChild(@PathVariable(value = "industryId", required = false) Long industryId)
    {
        List<Industry> industrys = industryService.selectIndustryList(new Industry());
        industrys.removeIf(p -> p.getIndustryId().intValue() == industryId || ArrayUtils.contains(StringUtils.split(p.getAncestors(), ","), industryId + ""));
        return success(industrys);
    }

    /**
     * 根据行业编号获取详细信息
     */
    @RequiresPermissions("basicdata:industry:query")
    @GetMapping(value = "/{industryId}")
    public AjaxResult getInfo(@PathVariable Long industryId)
    {
        return success(industryService.selectIndustryById(industryId));
    }

    /**
     * 新增行业
     */
    @RequiresPermissions("basicdata:industry:add")
    @Log(title = "行业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Industry industry)
    {
        if (!industryService.checkIndustryNameUnique(industry))
        {
            return error("新增行业'" + industry.getIndustryName() + "'失败，行业名称已存在");
        }
        industry.setCreateBy(SecurityUtils.getUsername());
        return toAjax(industryService.insertIndustry(industry));
    }

    /**
     * 修改行业
     */
    @RequiresPermissions("basicdata:industry:edit")
    @Log(title = "行业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Industry industry)
    {
        Long industryId = industry.getIndustryId();
        if (!industryService.checkIndustryNameUnique(industry))
        {
            return error("修改行业'" + industry.getIndustryName() + "'失败，行业名称已存在");
        }
        else if (industry.getParentId().equals(industryId))
        {
            return error("修改行业'" + industry.getIndustryName() + "'失败，上级行业不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, industry.getStatus()) && industryService.selectNormalChildrenIndustryById(industryId) > 0)
        {
            return error("该行业包含未停用的子行业！");
        }
        industry.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(industryService.updateIndustry(industry));
    }

    /**
     * 删除行业
     */
    @RequiresPermissions("basicdata:industry:remove")
    @Log(title = "行业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{industryId}")
    public AjaxResult remove(@PathVariable Long industryId)
    {
        if (industryService.hasChildByIndustryId(industryId))
        {
            return warn("存在下级行业,不允许删除");
        }
        return toAjax(industryService.deleteIndustryById(industryId));
    }

    /**
     * 获取部门树列表
     */
    @RequiresPermissions("basicdata:industry:list")
    @GetMapping("/industryTree")
    public AjaxResult industryTree(Industry industry)
    {
        return success(industryService.selectIndustryTreeList(industry));
    }
}
